package com.omesoft.hksnore.util.snore;

import android.content.Context;
import android.os.Vibrator;
import android.util.Log;

import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;

/**
 * Created by Omesoft on 2017/3/5.
 */

public class VbUtil {

    private Context context;
    private Vibrator vibrator;
    private long[] pattern;

    public VbUtil(Context mContext) {
        this.context = mContext;
        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void closeVb() {
        if (vibrator != null) {
            vibrator.cancel();
        }
    }

    public void loadVb(int level) {
        /**
         * 判断用户是否打开震动
         */
        if (!MySharedPreferencesUtil.getSwitch(context)) {
            return;
        }

        /**
         * 先关闭震动
         */
        if (vibrator != null) {
            vibrator.cancel();
        }

        /**
         * 震动时长
         */
        long vbOff = 0;//间隔时长
        long vbOn = 0;//振动时长
        int count = 0;//次数
        Log.e("level", "未处理的 level   ::   " + level);
        Log.e("level",
                "MySharedPreferencesUtil.getStrength(context)   ::   "
                        + MySharedPreferencesUtil.getStrength(context));
        int tempLevel = MySharedPreferencesUtil.getStrength(context) + level;
        Log.e("level", "处理过的 level   1::   " + tempLevel);
        tempLevel = tempLevel >= 9 ? 9 : tempLevel;
        Log.e("level", "处理过的 level   2::   " + tempLevel);
        tempLevel = tempLevel < 1 ? 1 : tempLevel;
        Log.e("level", "处理过的 level   3::   " + tempLevel);
        switch (tempLevel) {
            /**
             * 第一等级
             */
            case 1:
                vbOff = 350;
                vbOn = 2000;
                count = 3;
                break;
            case 2:
                vbOff = 350;
                vbOn = 1800;
                count = 4;
                break;
            case 3:
                vbOff = 350;
                vbOn = 1600;
                count = 5;
                break;
            /**
             * 第二等级
             */
            case 4:
                vbOff = 250;
                vbOn = 1200;
                count = 6;
                break;
            case 5:
                vbOff = 250;
                vbOn = 1100;
                count = 7;
                break;
            case 6:
                vbOff = 250;
                vbOn = 1000;
                count = 8;
                break;
            /**
             * 第三等级
             */
            case 7:
                vbOff = 200;
                vbOn = 700;
                count = 20;
                break;
            case 8:
                vbOff = 200;
                vbOn = 500;
                count = 21;
                break;

            default:// 这里相当于 case 9
                vbOff = 200;
                vbOn = 400;
                count = 22;
                break;
        }
        int length = count * 2;
        pattern = new long[length];
        for (int i = 0; i < length; i++) {
            if (i % 2 == 0) {
                pattern[i] = vbOff;
            } else {
                pattern[i] = vbOn;
            }
        }
        /**
         * long milliseconds ：震动的时长，单位是毫秒 long[] pattern ：自定义震动模式
         * 。数组中数字的含义依次是[静止时长，震动时长，静止时长，震动时长。。。]时长的单位是毫秒 boolean isRepeat ：
         * 是否反复震动，如果是true，反复震动，如果是false，只震动一次
         */
        // 下边是可以使震动有规律的震动 -1：表示不重复 0：循环的震动
        vibrator.vibrate(pattern, -1);
    }
}
